<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="SPU名称"> {{ spu.spuName }}</el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input
          placeholder="价格(元)"
          v-model="skuInfo.price"
          type="number"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input type="textarea" v-model="skuInfo.skuDesc" rows="4"></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" label-width="80px" ref="form">
          <!-- v-model="attr.attrIdAndAttrValueId" 收集用户所选的平台属性id&属性值id(以便后期提交服务器)  -->
          <el-form-item
            :label="attr.attrName"
            v-for="attr in attrInfoList"
            :key="attr.id"
          >
            <el-select
              placeholder="请选择"
              v-model="attr.attrIdAndAttrValueId"
              value
            >
              <el-option
                :label="attrValue.valueName"
                :value="`${attr.id}:${attrValue.id}`"
                v-for="attrValue in attr.attrValueList"
                :key="attrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form label-width="80px" :inline="true">
          <!-- v-model="saleAttr.saleAttrIdAndValueId" 收集用户所选的销售属性id&销售属性值id(以便后期提交服务器)  -->
          <el-form-item
            :label="saleAttr.saleAttrName"
            v-for="saleAttr in spuSaleAttrList"
            :key="saleAttr.id"
          >
            <el-select
              placeholder="请选择"
              v-model="saleAttr.saleAttrIdAndValueId"
            >
              <el-option
                :label="saleAttrValue.saleAttrValueName"
                :value="`${saleAttr.id}:${saleAttrValue.id}`"
                v-for="saleAttrValue in saleAttr.spuSaleAttrValueList"
                :key="saleAttrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table
          style="width: 100%"
         
          :data="spuImageList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            prop="prop"
            label="label"
            width="80px"
         
          >
          </el-table-column>
          <el-table-column
            prop="prop"
            label="图片"
            width="width"
            
          >
            <template slot-scope="{ row }">
              <img
                :src="row.imgUrl"
                alt=""
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
          <el-table-column prop="" label="名称" width="width" >
            <template slot-scope="{ row }">
              <h4>{{ row.imgName }}</h4>
            </template>
          </el-table-column>
          <el-table-column
            prop="prop"
            label="操作"
            width="width"
          
          >
            <template slot-scope="{ row }">
              <el-button
                type="success"
                v-if="row.isDefault == 0"
                @click="changeDefault(row)"
                >设置默认</el-button
              >
              <el-button v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
  
  <script>
export default {
  name: "MySkuFrom",
  data() {
    return {
      // 初始 图片|销售|平台 属性
      spuImageList: [],
      spuSaleAttrList: [],
      attrInfoList: [],
      // 收集sku数据字段
      skuInfo: {
        // 第一类收集数据 父给的数据
        spuId: 0,
        tmId: 0,
        category3Id: 0,
        // 第二类数据需要v-model双向绑定收集
        skuName: "",
        weight: "",
        price: 0,
        skuDesc: "",
        // 第三类收集的数据需要自己书写代码
        // 默认图片
        skuDefaultImg: "",
        // 收集图片的字段
        skuImageList: [
          // {
          //   id: 0,
          //   imgName: "",
          //   imgUrl: "",
          //   isDefault: "",
          //   skuId: 0,
          //   skuImgId: 0,
          // },
        ],
        // 平台属性
        skuAttrValueList: [
          // {
          //   // attrId: 0,
          //   // valueId: 0,
          // },
        ],
        //销售属性
        skuSaleAttrValueList: [
          // {
          //   id: 0,
          //   saleAttrId: 0,
          //   saleAttrName: "",
          //   saleAttrValueName: "",
          //   skuId: 0,
          //   spuId: 0,
          // },
        ],
      },
      spu: {},
      imageList: [],
    };
  },
  methods: {
    async getData(category1Id, category2Id, spu) {
      // 收集父组件给的数据
      console.log(spu);
      this.spu = spu;
      this.skuInfo.spuId = spu.id;
      this.skuInfo.tmId = spu.tmId;
      this.skuInfo.category3Id = spu.category3Id;
      // 初始化请求数据
      let result = await this.$API.spu.reqSpuImageList(spu.id);
      if (result.code === 200) {
        let list = result.data;
        // 给所有图片数据添加isDefault字段
        list.forEach((element) => {
          element.isDefault = 0;
        });
        this.spuImageList = list;
      }
      let result1 = await this.$API.spu.reqSpuSaleAttrList(spu.id);
      if (result1.code === 200) {
        this.spuSaleAttrList = result1.data;
      }
      let result2 = await this.$API.spu.reqAttrInfoList(
        category1Id,
        category2Id,
        spu.category3Id
      );
      if (result2.code === 200) {
        this.attrInfoList = result2.data;
      }
    },
    // 点击复选框时触发的回调
    handleSelectionChange(parms) {
      // 将用户选择的图片数据进行单独对象收集（后期提交服务器在进行合并整理）
      this.imageList = parms;
    },
    // 设置默认图片||默认图片按钮（排他）
    changeDefault(row) {
      this.spuImageList.forEach((item) => {
        item.isDefault = 0;
      });
      row.isDefault = 1;
      this.skuInfo.skuDefaultImg = row.imgUrl;
    },
    // 点击取消按钮 通知父组件切换
    cancel() {
      this.$emit("changeScencs", 0);
      // 点击取消 清楚上次用户残留数据
      Object.assign(this._data, this.$options.data());
    },
    // 整理最终参数
    async save() {
      // // 解构出临时存放参数的对象
      const { attrInfoList, spuSaleAttrList, skuInfo, imageList } = this;
      // 平台属性
      skuInfo.skuAttrValueList = attrInfoList.reduce((prve, item) => {
        if (item.attrIdAndAttrValueId) {
          const [attrId, valueId] = item.attrIdAndAttrValueId.split(":");
          prve.push({ attrId, valueId });
        }
        return prve;
      }, []);
      // 销售属性
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prve, item) => {
        if (item.saleAttrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] =
            item.saleAttrIdAndValueId.split(":");
          prve.push({ saleAttrId, saleAttrValueId });
        }
        return prve;
      }, []);
      // 图片数据
      skuInfo.skuImageList = imageList.map((item) => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        };
      });
      // 发请求
      // console.log(skuInfo.skuImageList,skuInfo);
      let result = await this.$API.spu.reqAddSku(skuInfo);
      if (result.code === 200) {
        this.$message({
          type: "success",
          message: "添加SKU成功",
        });
        this.$emit("changeScencs", 0);
      }
    },
  },
};
</script>
  
  <style>
</style>